package controller;
import javax.swing.*;
import multiformat.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * De controller voor de "Format" van de calculator.
 * @author Minkey
 */
public class FormatController extends JPanel implements ActionListener{
    private	JComboBox formatComboBox;
    Calculator calc;
    
    /**
     * De contructor.
     * @param calc
     */
	public FormatController(Calculator calc){
		this.calc = calc;
		formatComboBox = new JComboBox(new String [] {"Rational", "Float", "Fixed"});
		formatComboBox.setSelectedItem("Fixed");
		formatComboBox.setPreferredSize(new Dimension(150,20));
		
		formatComboBox.addActionListener(this);
		this.add(formatComboBox);
	}

	/**
	 * ActionPerformed, hier wordt geluisterd naar een action. Als er een action is uitgevoerd wordt deze hier afgehandeld.
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == formatComboBox){
			
			JComboBox temp = (JComboBox) e.getSource();
			
			if(temp.getSelectedItem() == "Rational"){
				calc.setFormat(new RationalFormat());
			} else if(temp.getSelectedItem() == "Float"){
				calc.setFormat(new FloatingPointFormat());
			} else if(temp.getSelectedItem() == "Fixed"){
				calc.setFormat(new FixedPointFormat());
			}
		}
	}
}
	
